package com.zzy.demo.service.feign.impl;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzy.cms.common.reuslt.Result;
import com.zzy.cms.common.reuslt.ResultCode;
import com.zzy.demo.entity.Division;
import com.zzy.demo.enums.BusinessResultCode;
import com.zzy.demo.feign.DivisionFeignService;
import com.zzy.demo.service.DivisionService;
import com.zzy.demo.vo.DivisionVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * 区划SDK实现类 provider-implements
 *
 * @author weiyuan
 * @date 20240117
 */
@Api(tags = "行政区划SDK")
@Slf4j
@RestController
@Component
public class DivisionFeignServiceImpl implements DivisionFeignService {

    @Resource
    private DivisionService topsdpDivisionService;

    @ApiOperation("根据区划编码查询")
    @Override
    public Result<DivisionVo> findByDivCode(String divCode) {
        log.info("DivisionFeignServiceImpl.findByDivCode req={}", divCode);
        if (StringUtils.isBlank(divCode)) {
            return Result.error(ResultCode.CODE_C0400);
        }
        try {
            LambdaQueryWrapper<Division> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(Division::getDivCode, divCode);
            Division result = topsdpDivisionService.selectOne(lambdaQuery);
            if (result == null) {
                return Result.success();
            }
            DivisionVo divisionVo = new DivisionVo();
            BeanUtils.copyProperties(result, divisionVo);
            return Result.success(divisionVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("DivisionFeignServiceImpl.findByDivCode error is ", e);
            return Result.error(BusinessResultCode.TOPDP_T0001);
        }
    }
}
